package top.callname.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;

import top.callname.bean.AdminBean;
import top.callname.bean.AdminPageList;
import top.callname.bean.ConstanstBean;

public class AdminDao extends BaseDao{

	Logger logger = Logger.getLogger(AdminDao.class);
	
	public boolean deleteFromAdminByPid(Integer pid){
		String sql = "delete from t_admin where pid="+pid;
		logger.info("从t_admin删除用户："+sql);
		return adu(sql);
	}

	public boolean deleteFromAdminClassByPid(Integer pid){
		String sql = "delete from t_admin_class where pid="+pid;
		logger.info("从 t_admin_class 删除关系："+sql);
		return adu(sql);
	}
	public boolean deleteFromAdminClassByCid(Integer cid){
		String sql = "delete from t_admin_class where cid="+cid;
		logger.info("从 t_admin_class 删除关系："+sql);
		return adu(sql);
	}
	
	public boolean insertAdminClass(Integer pid ,String cid ){
		String sql = "insert into t_admin_class (pid,cid) values ( "+pid+","+cid+" ) ";
		logger.info("新增 t_admin_class 关系："+sql);
		return adu(sql);
	}
	
	
	public boolean updateByPid(AdminBean adminBean){
		String sql = "update t_admin set username='"+adminBean.getUsername()+"',"
				+ "fullname='"+adminBean.getFullname()+"',"
						+ "passwd='"+adminBean.getPasswd()+"' "
								+ "where pid="+adminBean.getPid();
		return adu(sql);
	}
	
	public AdminPageList selectByPage(int pageNow){
		AdminPageList pageList = new AdminPageList();
		pageList.setPageNow(pageNow);
		//查询总记录
		int totalSize = getCount("t_admin");
		pageList.setTotalSize(totalSize);
		
		ArrayList<AdminBean> list = new ArrayList<AdminBean>();
		
		String limit = "order by pid desc limit "+pageList.getStart()+","+pageList.getOffset();
		String sql =  "select  * from t_admin "+limit;
		
		logger.info("分页查询管理员（教师）信息："+sql);
		res = query(StringEscapeUtils.escapeSql(sql));
		try {
			while(res.next()){
				AdminBean bean = new AdminBean();
				bean.setPid(res.getInt("pid"));
				bean.setUsername(res.getString("username"));
				bean.setFullname(res.getString("fullname"));
				bean.setPasswd(res.getString("passwd"));
				bean.setRole(res.getInt("role"));
				bean.setCreatetime(res.getString("createtime"));
				bean.setOpenid(res.getString("openid"));
				bean.setRoleName(ConstanstBean.ROLE_MAP.get(bean.getRole()));
				list.add(bean);
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_admin 出错："+e);
		}
		pageList.setList(list);
		return pageList;
	}
	
	/**
	 * 用户注册
	 * @param id
	 * @param ipaddress
	 * @return boolean
	 */
	public boolean adminRegister(AdminBean adminBean){
		String sql =  "insert into  t_admin  (username,passwd,fullname,createtime) values ('"
							+adminBean.getUsername()+"','"
							+adminBean.getPasswd()+"','"
							+adminBean.getFullname()+"',NOW() )";
		return adu(sql);
	}
	
	public AdminBean selectByPid(Integer pid){
		String sql =  "select  *  from t_admin where pid="+pid;
		res = query(sql);
		AdminBean bean = new AdminBean();
		try {
			if(res.next()){
				bean.setPid(res.getInt("pid"));
				bean.setUsername(res.getString("username"));
				bean.setFullname(res.getString("fullname"));
				bean.setPasswd(res.getString("passwd"));
				bean.setRole(res.getInt("role"));
				bean.setCreatetime(res.getString("createtime"));
				bean.setOpenid(res.getString("openid"));
				bean.setRoleName(ConstanstBean.ROLE_MAP.get(bean.getRole()));
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("根据PID查询出错："+e);
		}
		return bean;
	}
	
	public AdminBean selectUname(String uname){
		String sql =  "select  *  from t_admin where username='"+uname+"' limit 0,1";
		res = query(sql);
		try {
			AdminBean bean = null;
			if(res.next()){
				bean = new AdminBean();
				bean.setPid(res.getInt("pid"));
				bean.setUsername(res.getString("username"));
				bean.setFullname(res.getString("fullname"));
				bean.setPasswd(res.getString("passwd"));
				bean.setRole(res.getInt("role"));
				bean.setCreatetime(res.getString("createtime"));
				bean.setOpenid(res.getString("openid"));
				bean.setRoleName(ConstanstBean.ROLE_MAP.get(bean.getRole()));
			}
			closeDB();
			return bean;
		} catch (SQLException e) {
			logger.error("根据用户名查询出错："+e);
		}
		return null;
	}
	/**
	 * 根据用户名密码查询
	 * @param uname
	 * @param upass
	 * @return AdminBean
	 */
	public AdminBean selectAdminByPwdAndUname(String uname,String upass){
		String sql =  "select  *  from t_admin where username='"+uname+"' and passwd='"+upass+"'";
		res = query(sql);
		try {
			AdminBean bean = null;
			if(res.next()){
				bean = new AdminBean();
				bean.setPid(res.getInt("pid"));
				bean.setUsername(res.getString("username"));
				bean.setFullname(res.getString("fullname"));
				bean.setPasswd(res.getString("passwd"));
				bean.setRole(res.getInt("role"));
				bean.setCreatetime(res.getString("createtime"));
				bean.setOpenid(res.getString("openid"));
				bean.setRoleName(ConstanstBean.ROLE_MAP.get(bean.getRole()));
			}
			closeDB();
			logger.info("管理员登录：uname:"+uname);
			return bean;
		} catch (SQLException e) {
			logger.error("管理员登录出错："+e);
		}
		return null;
	}
	

}
